package com.hmusic.mymusic.service.impl;

import com.hmusic.mymusic.dao.SingerMapper;
import com.hmusic.mymusic.domain.Consumer;
import com.hmusic.mymusic.domain.Singer;
import com.hmusic.mymusic.service.SingerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * 歌手service实现类
 */
@Service
public class SingerServiceImpl implements SingerService {

    @Autowired
    private SingerMapper singerMapper;
    @Value("${singerAvator.dir}")
    private String realPath;

    /**
     * 添加歌手
     *
     * @param singer
     */
    @Override
    public void insert(Singer singer) {
        try{
            List<Singer> singerList = singerMapper.singerOfName(singer.getName());
            if (singerList.size()>0){
                throw new RuntimeException("歌手已存在");
            }else {
                singerMapper.insert(singer);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }

    }

    /**
     * 修改歌手
     *
     * @param singer
     */
    @Override
    public void update(Singer singer) {
        singerMapper.update(singer);
    }

    /**
     * 删除歌手
     *
     * @param id
     */
    @Override
    public void delete(String id) {
        try{
            Singer singer = singerMapper.selectByPrimaryKey(id);
            String oldSingerAvatorUrl = singer.getPic();
            String newSingerAvatorUrl = oldSingerAvatorUrl.substring(15);
            if (!newSingerAvatorUrl.equals("Firstavator.jpeg")){
                File file=new File(realPath,newSingerAvatorUrl);
                if(file.exists())file.delete();//删除原有头像地址
            }
            singerMapper.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据主键查询整个对象
     *
     * @param id
     */
    @Override
    public Singer selectByPrimaryKey(String id) {
        try {
            singerMapper.updateClick(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return singerMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询所有歌手
     */
    @Override
    public List<Singer> allSinger() {
        return singerMapper.allSinger();
    }

    /**
     * 根据歌手名模糊查询
     *
     * @param name
     */
    @Override
    public List<Singer> singerOfName(String name) {
        return singerMapper.singerOfName(name);
    }

    /**
     * 根据歌手性别模糊查询
     *
     * @param sex
     */
    @Override
    public List<Singer> singerOfSex(String sex) {
        return singerMapper.singerOfSex(sex);
    }

    /**
     * 计算歌手总数
     */
    @Override
    public int singerCount() {
        return singerMapper.singerCount();
    }

    /**
     * 首页热门歌手
     *
     * @return
     */
    @Override
    public List<Singer> hotSinger() {
        return singerMapper.hotSinger();
    }

    /**
     * 点击量增加
     *
     * @param id
     */
    @Override
    public void updateClick(String id) {
        singerMapper.updateClick(id);
    }
}
